package com.game77.guessTheWords2.layer;

import android.util.Log;
import android.widget.Toast;

import com.game77.guessTheWords2.MainActivity;
import com.game77.guessTheWords2.R;
import com.game77.guessTheWords2.core.GameSystem;
import com.game77.guessTheWords2.core.StageManager;
import com.game77.guessTheWords2.scene.ClassChooseScene;
import com.game77.guessTheWords2.scene.GameScene;
import com.game77.guessTheWords2.scene.ToolShopScene;
import com.game77.guessTheWords2.util.ADHelper;
import com.game77.guessTheWords2.util.AudioHelper;
import com.game77.guessTheWords2.util.GameUtil;
import com.umeng.fb.FeedbackAgent;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYPoint;

/**
 * 主菜单层
 * 
 * @author Administrator
 * 
 */
public class MainLayer extends Layer {

	Sprite background;

	Button btn_start;
	Button btn_continue;
	Button btn_Sound_on;
	Button btn_Sound_off;
	Button btn_feedback;

	Label studioInfo;

	public MainLayer() {
		// 背景
		background = Sprite.make(GameSystem.tex_main_background);
		background.setAnchor(0, 0);
		background.setPosition(WYPoint.make(0, 0));
		this.addChild(background);

		loadButtons();

		// 检查更新
		MainActivity.instance.UpdateVersion();
	}

	private void loadButtons() {
		btn_start = Button.make(R.drawable.btn_start,
				R.drawable.btn_start_press, this, "onNewGameClicked");
		btn_start.setAnchorX(1);
		btn_start.setPosition(438, 453);
		this.addChild(btn_start);

		btn_continue = Button.make(R.drawable.btn_continue,
				R.drawable.btn_continue_press, this, "onContinueClicked");
		btn_continue.setAnchorX(1);
		btn_continue.setPosition(438, 324);
		this.addChild(btn_continue);

		btn_Sound_on = Button.make(R.drawable.btn_sound_on,
				R.drawable.btn_sound_on, this, "onSoundOnClicked");
		btn_Sound_on.setPosition(54, 116);
		this.addChild(btn_Sound_on);

		btn_Sound_off = Button.make(R.drawable.btn_sound_off,
				R.drawable.btn_sound_off, this, "onSoundOffClicked");
		btn_Sound_off.setPosition(54, 116);
		this.addChild(btn_Sound_off);

		btn_feedback = Button.make(R.drawable.btn_feedback,
				R.drawable.btn_feedback, this, "onFeedbackClicked");
		btn_feedback.setClickScale(1.1f);
		btn_feedback.setPosition(54, 48);
		this.addChild(btn_feedback);

		setSoundStatus();
	}

	public void onFeedbackClicked() {
		AudioHelper.clickButton();
		MainActivity.instance.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				FeedbackAgent agent = new FeedbackAgent(MainActivity.instance);
				agent.startFeedbackActivity();
			}
		});

	}

	private void setSoundStatus() {
		if (StageManager.instance.isAudioOn()) {
			AudioHelper.playBackgoundMusic();
			btn_Sound_off.setVisible(false);
			btn_Sound_on.setVisible(true);
		} else {
			AudioHelper.stopBackgoundMusic();
			btn_Sound_off.setVisible(true);
			btn_Sound_on.setVisible(false);
		}

	}

	public void onSoundOffClicked() {
		StageManager.instance.setAudioOn(true);
		setSoundStatus();
	}

	public void onSoundOnClicked() {
		StageManager.instance.setAudioOn(false);
		setSoundStatus();
	}

	public void onNewGameClicked() {
		Log.d("click", "new game");
		AudioHelper.clickButton();
		GameUtil.switchSceneNormalFast(ClassChooseScene.make());
		ClassChooseScene.make().refreshClassInfos();
		ClassChooseScene.make().updateCoins();
		ADHelper.hideAd();
	}

	public void onContinueClicked() {
		Log.d("click", "continue");
		AudioHelper.clickButton();
		if (StageManager.instance.getLastStage() > 0) {
			GameUtil.switchSceneFlipX(GameScene.make());
			GameLayer.instance.startGame(StageManager.instance.getLastStage());
			// 显示广告
			ADHelper.showAd();
		} else {
			MainActivity.instance.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(MainActivity.instance, "没有保存的游戏记录",
							Toast.LENGTH_LONG).show();
				}
			});

		}
	}

	public void onShopClicked() {
		Log.d("click", "Shop");
		GameUtil.switchSceneNormalFast(ToolShopScene.make());
		ToolShopScene.make().updateCoins();
	}

}
